#define GLOBAL_CPP
#include "global.h"

#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QDateTime>
#include <QCoreApplication>

void MessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    Q_UNUSED(context);
    QString logDir = QCoreApplication::applicationDirPath() + "/log";
    QDir().mkpath(logDir);
    QFile log(logDir+"/"+QDate::currentDate().toString("yyyyMMdd")+".txt");
    if (log.open(QIODevice::Append|QIODevice::Text)) {
        QTextStream out(&log);
        out << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ");
        switch (type) {
        case QtDebugMsg:
            out << "Debug: ";
            break;
        case QtInfoMsg:
            out << "Info: ";
            break;
        case QtWarningMsg:
            out << "Warning: ";
            break;
        case QtCriticalMsg:
            out << "Critical: ";
            break;
        case QtFatalMsg:
            out << "Fatal: ";
            abort();
        }
        out << msg << endl;
    }
}
